#include "../tools.h"
#include <iostream>
using std::cout;


/*有一种巧妙的方法，可以不用显式获取数组长度*/
class Solution {
public:
    int list_size(ListNode *node)
    {
        int len = 0;
        while(node != nullptr)
        {
            ++len;
            node = node->next;
        }
        return len;
    }
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        int lenA = list_size(headA);
        int lenB = list_size(headB);
        int lenDiff;
        if (lenA > lenB) {
            lenDiff = lenA - lenB;
            for(int i = 0; i < lenDiff; ++i) headA = headA->next;
        }
        else {
            lenDiff = lenB - lenA;
            for(int i = 0; i < lenDiff; ++i) headB = headB->next;
        }

        while(headA != nullptr){
            if(headA == headB) break;
            headA = headA->next;
            headB = headB->next;
        }
        return headA;
    }
};


int main(int argc, char const *argv[])
{
    ListNode *p = simple_list();

    Solution s;
    cout << s.list_size(p) << std::endl;
    show(p);


    return 0;
}
